/**
 * @Author: 17197
 * @Date: 2024-05-08 00:18:26
 * @Description: multer.js
 * @Version: 1.0
 * @Last Modified time : 2024-05-08 00:18:26
 **/
const multer = require('multer');
const path = require('path');
const mkdirp = require('mkdirp');

const storage = multer.diskStorage({
	destination: (req, file, cb) => { // 上传文件目录
		// 从req.body.fileName解析出目录路径
		const directoryPath = path.dirname(req.body.fileName);

		// 拼接上传基础路径和解析出的目录路径
		const uploadPath = path.join(__dirname, '..', 'uploads', directoryPath);

		// 使用mkdirp模块递归创建目录
		const made = mkdirp.sync(uploadPath)
		console.log(`made directories, starting with ${made}`)
	},
	filename: (req, file, cb) => { // 上传文件名
		const fileName = path.basename(req.body.fileName);
		cb(null, fileName); // 使用时间戳和原文件扩展名作为文件名
	},
});
const upload = multer({storage: storage});
module.exports = upload;
